package com.hzdl.apple.controller.backCotroll;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.hzdl.apple.bean.Good;
import com.hzdl.apple.bean.User;
import com.hzdl.apple.service.back.BackGoodsService;
import com.hzdl.apple.utils.HttpUtils;
import com.jspsmart.upload.File;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

import net.sf.json.JSONObject;
@WebServlet("/BackGoods/*")
public class GoodsServlet extends HttpServlet {
	BackGoodsService service;
	@Override
	public void init() throws ServletException {
		super.init();
		service = new BackGoodsService();
		getServletContext().setAttribute("path", getServletContext().getContextPath());
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		String baseUri = req.getRequestURI();
		JSONObject json = new JSONObject();
		if (baseUri.contains("/toGoodsEdit")) {
			List<Good> list = service.getAllGoods();
			req.setAttribute("data", JSON.toJSONString(list));
			HttpUtils.forward("back/back-GoodsEdit.jsp", req, resp);
		}else if(baseUri.contains("/AddGoods")){
			SmartUpload su = new SmartUpload();
			su.initialize(getServletConfig(), req, resp);
			su.setAllowedFilesList("jpeg,jpg,gif,png");
			try {
				su.upload();
			} catch (SmartUploadException e) {
				e.printStackTrace();
			}
			File file = su.getFiles().getFile(0);
			try {
				file.saveAs("E:/AppleStore/Apple/Apple/WebContent/img/imgs/"+file.getFileName());
				
			} catch (SmartUploadException e) {w
				e.printStackTrace();
			}
			String goodsName = su.getRequest().getParameter("goodsName");
			String goodsColor = su.getRequest().getParameter("goodsColor");
			String goodsImgForShow = su.getRequest().getParameter("goodsImgForShow");
			String goodsInfo = su.getRequest().getParameter("goodsInfo");
			String goodsImg = file.getFileName();
			String iPadConn = su.getRequest().getParameter("iPadConn");
			int macDisc = Integer.parseInt(su.getRequest().getParameter("macDisc"));
			double price = Double.parseDouble(su.getRequest().getParameter("price"));
			int num = Integer.parseInt(su.getRequest().getParameter("num"));
			Good good = new Good(goodsName, goodsColor, goodsImgForShow, goodsInfo, goodsImg, iPadConn, macDisc, price, num);
			boolean b = service.addGoods(good);
			if(b){
			json.put("success", "true");
			resp.getWriter().write(json.toString());
			}else{
				json.put("error", "false");
				resp.getWriter().write(json.toString());
			}
		}else if(baseUri.contains("/UpdateGoods")){
		
			String GoodsID = req.getParameter("goodsID");
			String goodsName = req.getParameter("goodsName");
			String goodsColor = req.getParameter("goodsColor");
			String goodsImgForShow = req.getParameter("goodsImgForShow");
			String goodsInfo = req.getParameter("goodsInfo");
			String goodsImg = req.getParameter("goodsImg");
			String iPadConn = req.getParameter("iPadConn");
			int macDisc = Integer.parseInt(req.getParameter("macDisc"));
			double price = Double.parseDouble(req.getParameter("price"));
			int num = Integer.parseInt(req.getParameter("num"));
			Good good = new Good(goodsName, goodsColor, goodsImgForShow, goodsInfo, goodsImg, iPadConn, macDisc, price, num);
			boolean b = service.updateGoods(GoodsID, good);
			if(b){
				json.put("success", "true");
				resp.getWriter().write(json.toString());
			}else{
				json.put("error", "false");
				resp.getWriter().write(json.toString());
			}
		}else if(baseUri.contains("/DeleteGoods")){
			String GoodsID = req.getParameter("goodsID");
			boolean b = service.deleteGoods(GoodsID);
			if(b){
				json.put("success", "true");
				resp.getWriter().write(json.toString());
			}else{
				json.put("error", "false");
				resp.getWriter().write(json.toString());
			}
		}
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}
